#!/usr/bin/env bash
# Copyright (c) 2023 maminjie <canpool@163.com>
# SPDX-License-Identifier: MulanPSL-2.0


__ola_init() {
    local boolean_options="
        --help -h
        --force -f
    "
    case "$cur" in
        -*)
            COMPREPLY=($(compgen -W "$boolean_options" -- "$cur"))
            ;;
    esac
}

__ola_abi() {
    local subcommands="
        help
        sodiff
        pkgdiff
        bincheck
    "
    case "$cur" in
        *)
            COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
            ;;
    esac
}

__ola_gec() {
    local subcommands="
        help
        push
        fork
        delete
        createpr
        commentpr
        ls
    "
    case "$cur" in
        *)
            COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
            ;;
    esac
}

__ola_git() {
    local subcommands="
        help
        logexp
        alias
        key
    "
    case "$cur" in
        *)
            COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
            ;;
    esac
}

__ola_spec() {
    local subcommands="
        help
        prep
        update
        date
    "
    case "$cur" in
        *)
            COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
            ;;
    esac
}

__ola_obs() {
    local subcommands="
        help
        ls
        branch
        checkout
        importsrcpkg
        update
        setflag
        buildinfo
        getbin
        getbinaries
        getbinlist
        getfile
        unresolved
        pkgresults
        getbuildlogs
        getjobtime
        rdelete
        rebuild
        config
    "
    case "$cur" in
        *)
            COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
            ;;
    esac
}

__ola_oec() {
    local subcommands="
        help
        finddeveloper
        patchinfo
        release
    "
    case "$cur" in
        *)
            COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
            ;;
    esac
}

__ola_ship() {
    local subcommands="
        help
        provides
        requires
        whatprovides
        whatrequires
        requiredby
        builddep
    "
    case "$cur" in
        *)
            COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
            ;;
    esac
}

__ola_sig() {
    local subcommands="
        help
        ls
        pkg
        owner
        find
        maintainer
    "
    case "$cur" in
        *)
            COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
            ;;
    esac
}

__ola_sync() {
    local options="
        --help -h
        --source -s
        --target -t
        --repo -r
        --owner -o
        --repo-list
        --branch-list
    "

    local subcommands="
        help
        check
        diff
        log
        version
    "
    case "$cur" in
        -*)
            COMPREPLY=($(compgen -W "$options" -- "$cur"))
            ;;
        *)
            COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
            ;;
    esac
}

__ola_patcher() {
    local subcommands="
        help
        check
        group
        info
        diff
        stat
    "
    case "$cur" in
        *)
            COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
            ;;
    esac
}

__ola_rpm() {
    local subcommands="
        help
        diff
        cmp
        find
        child
        gendep
        finddep
        srclist
    "
    case "$cur" in
        *)
            COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))
            ;;
    esac
}

__ola_ola() {
    # global options that may appear after the ola command
    local boolean_options="
        --help -h
        --version -v
    "

    case "$cur" in
        -*)
            COMPREPLY=($(compgen -W "$boolean_options" -- "$cur"))
            ;;
        *)
            COMPREPLY=($(compgen -W "${commands[*]}" -- "$cur"))
            ;;
    esac
}

__ola() {
    local common_commands=(
        call
        checkenv
        debug
        help
        init
        setting
    )

    local app_commands=(
        abi
        gec
        git
        spec
        unittest
        yumpkg
    )

    local plugin_commands=(
        cloc
        nvd
        obs
        oec
        packageinit
        patcher
        repo
        rpm
        ship
        sig
        sync
        uncompress
    )

    local commands=(${common_commands[*]} ${app_commands[*]} ${plugin_commands[*]})

    local cur prev words cword
    _get_comp_words_by_ref -n : cur prev words cword

    local command='ola' command_pos=0 subcommand_pos
    local counter=1
    while [ "$counter" -lt "$cword" ]; do
        case "${words[$counter]}" in
            ola)
                return 0
                ;;
            -*)
                ;;
            *)
                command="${words[$counter]}"
                command_pos=$counter
                break
                ;;
        esac
        ((counter++))
    done

    local completions_func=__ola_${command//-/_}
    declare -F $completions_func >/dev/null && $completions_func

    return 0
}

complete -F __ola ola
